package com.zj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zj.common.Utility;
import com.zj.entity.Goods;
import com.zj.entity.UserInfo;
import com.zj.mapper.GoodsMapper;
import com.zj.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 商品服务impl
 *
 * @author zzj
 * @date 2023/02/23
 */
@Service
@Transactional
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements GoodsService {
    @Autowired
    private GoodsMapper goodsMapper;

    /**
     * 添加商品
     *
     * @param userInfo 用户信息
     * @param goods    货物
     */
    public void addGoods(UserInfo userInfo, Goods goods) {
        goods.setOwnUserId(userInfo.getUserId());
        goods.setCreateTime(Utility.getCurrentDate());
        goods.setUserId(userInfo.getUserId());
        goods.setUserName(userInfo.getNickname());
        goods.setGoodsStatus(1);
        goods.setUuid(Utility.getUUID());
        goodsMapper.insert(goods);
    }

    /**
     * 我都所有货物
     *
     * @param userInfo 用户信息
     * @return {@code List<Goods>}
     */
    public List<Goods> getMyGoods(UserInfo userInfo){
        QueryWrapper<Goods> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userInfo.getUserId());
        return goodsMapper.selectList(queryWrapper);
    }

    /**
     * 我卖出的产品
     *
     * @param userInfo 用户信息
     * @return {@code List<Goods>}
     */
    @Override
    public List<Goods> getMySellGoods(UserInfo userInfo) {
        QueryWrapper<Goods> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userInfo.getUserId()).ne("own_user_id",userInfo.getUserId());;
        return goodsMapper.selectList(queryWrapper);
    }

    /**
     * 我正在卖的货物（尚未卖出）
     *
     * @param userInfo 用户信息
     * @return {@code List<Goods>}
     */
    public List<Goods> getMySellingGoods(UserInfo userInfo) {
        QueryWrapper<Goods> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userInfo.getUserId()).eq("own_user_id",userInfo.getUserId());;
        return goodsMapper.selectList(queryWrapper);
    }

    /**
     * 我买入的商品
     *
     * @param userInfo 用户信息
     * @return {@code List<Goods>}
     */
    @Override
    public List<Goods> getMyBuyGoods(UserInfo userInfo) {
        QueryWrapper<Goods> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("own_user_id",userInfo.getUserId()).ne("user_id",userInfo.getUserId());
        return goodsMapper.selectList(queryWrapper);
    }


    /**
     * 查询我的货物  通过名称或描述
     *
     * @param userInfo 用户信息
     * @param queryKey 查询关键
     * @return {@code List<Goods>}
     */
    @Override
    public List<Goods> getMyGoodsByNameOrDescription(UserInfo userInfo,String queryKey) {
        QueryWrapper<Goods> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userInfo.getUserId()).and(wrapper->wrapper.like ("goods_name",queryKey).or().like("goods_description",queryKey));
        return goodsMapper.selectList(queryWrapper);
    }

    /**
     * 查询公共产品 通过名字 或 描述 或 用户名
     *
     * @param queryKey 查询关键
     * @return {@code List<Goods>}
     */
    @Override
    public List<Goods> selectPublicGoodsByNameOrDescriptionOrUserName(String queryKey) {
        QueryWrapper<Goods> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("goods_status",1).and(wrapper->wrapper.like("goods_name",queryKey).or().like("goods_description",queryKey).or().like("user_name",queryKey));
        return goodsMapper.selectList(queryWrapper);
    }
}
